package com.chat.controller;

import com.chat.domain.dto.user.UserPasswordDTO;
import com.chat.domain.dto.user.UserProfileDTO;
import com.chat.service.UserService;
import com.chat.util.Result;
import com.chat.util.SecurityHolderUtil;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 *
 * @author wb
 * @since 2024-08-21
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/friends")
    public Result<?> getFriends(HttpSession session) {
        return Result.ok(userService.getFriendsByUserId(SecurityHolderUtil.getUserId(session)));
    }

    @GetMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        return userService.removeById(id) ? Result.ok() : Result.fail();
    }


    @PostMapping("/update-profile")
    public Result<Boolean> updateProfile(@Valid UserProfileDTO userProfileDTO, HttpSession session) {
        return userService.updateProfile(userProfileDTO, session) ? Result.ok() : Result.fail();
    }


    @PostMapping("/update-password")
    public Result<Boolean> updatePassword(UserPasswordDTO userPasswordDTO, HttpSession session) {
        return userService.updatePassword(userPasswordDTO, session) ? Result.ok() : Result.fail();
    }

}
